package com.richard.dev.common.application;

import android.app.Application;

import com.richard.library.basic.crash.CrashHandler;
import com.richard.library.basic.eventbus.CrossProcessReceiver;
import com.richard.library.basic.util.LogUtil;
import com.richard.library.basic.util.ObjectUtil;
import com.richard.library.basic.util.ToastUtil;
import com.richard.library.context.AppContext;
import com.richard.library.http.interceptor.LogInterceptor;
import com.richard.library.http.manager.RetrofitClient;
import com.tencent.smtt.export.external.TbsCoreSettings;
import com.tencent.smtt.sdk.QbSdk;

import java.util.HashMap;

import okhttp3.Interceptor;

/**
 * <pre>
 * Description : common_lib 初始化
 * Author : admin-richard
 * Date : 2019-05-16 16:27
 * Changelog:
 * Version            Date            Author              Detail
 * ----------------------------------------------------------------------
 * 1.0         2019-05-16 16:27     admin-richard         new file.
 * </pre>
 */
public final class CommonInit {

    private static boolean isDebug;

    public CommonInit(boolean isDebug) {
        CommonInit.isDebug = isDebug;
    }

    public static boolean isDebug() {
        return isDebug;
    }

    public static Builder open() {
        return new Builder();
    }

    public static class Builder {

        private boolean isDebug;

        private Interceptor[] interceptorArray;

        private String retrofitBaseURL = "http://www/";

        private String logTag = "api";

        private boolean isEnabledCrashHandler;

        private boolean isEnabledX5TBS;

        private boolean isOpenCrossProcessEventBusReceiver;


        public Builder setDebug(boolean debug) {
            isDebug = debug;
            return this;
        }

        public Builder setInterceptorList(Interceptor[] interceptorArray) {
            this.interceptorArray = interceptorArray;
            return this;
        }

        public Builder setRetrofitBaseURL(String retrofitBaseURL) {
            this.retrofitBaseURL = retrofitBaseURL;
            return this;
        }

        public Builder setLogTag(String logTag) {
            this.logTag = logTag;
            return this;
        }

        public Builder setEnabledCrashHandler(boolean enabledCrashHandler) {
            isEnabledCrashHandler = enabledCrashHandler;
            return this;
        }

        public Builder setEnabledX5TBS(boolean enabledX5TBS) {
            isEnabledX5TBS = enabledX5TBS;
            return this;
        }

        public Builder setOpenCrossProcessEventBusReceiver(boolean openCrossProcessEventBusReceiver) {
            isOpenCrossProcessEventBusReceiver = openCrossProcessEventBusReceiver;
            return this;
        }

        public CommonInit build(Application application) {
            //初始化Context管理工具类
            AppContext.init(application,isDebug);

            //初始化Toast
            ToastUtil.init();

            //初始化异常统一处理器
            if (isEnabledCrashHandler) {
                CrashHandler.getInstance().init();
            }

            //初始化日志工具类
            LogUtil.getConfig()
                    .setGlobalTag(logTag)
                    .setLogHeadSwitch(false)
                    .setBorderSwitch(false)
                    .setLogSwitch(isDebug);

            //初始化网络请求库
            RetrofitClient retrofitClient = RetrofitClient.getInstance();
            retrofitClient.setBaseURL(retrofitBaseURL);
            retrofitClient.initIgnoreCer();
            if (ObjectUtil.isNotEmpty(interceptorArray)) {
                retrofitClient.addInterceptor(interceptorArray);
            }
            if (isDebug) {
                retrofitClient.addInterceptor(new LogInterceptor());
            }

            //初始化X5内核
            if (isEnabledX5TBS) {
                QbSdk.initX5Environment(application, new QbSdk.PreInitCallback() {
                    @Override
                    public void onCoreInitFinished() {

                    }

                    @Override
                    public void onViewInitFinished(boolean b) {

                    }
                });

                //为了提高内核占比，在初始化前可配置允许移动网络下载内核（大小 40-50 MB）。默认移动网络不下载
                QbSdk.setDownloadWithoutWifi(true);

                // 在调用TBS初始化、创建WebView之前进行如下配置
                HashMap<String, Object> map = new HashMap<>();
                map.put(TbsCoreSettings.TBS_SETTINGS_USE_SPEEDY_CLASSLOADER, true);
                map.put(TbsCoreSettings.TBS_SETTINGS_USE_DEXLOADER_SERVICE, true);
                QbSdk.initTbsSettings(map);
            }

            //初始化发送EventBus跨进程广播
            if (isOpenCrossProcessEventBusReceiver) {
                CrossProcessReceiver.init(application);
            }

            return new CommonInit(isDebug);
        }
    }
}
